package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.*;
import com.banmao.mall.model.bo.client.cart.CartAddBO;
import com.banmao.mall.model.bo.client.cart.CartCheckedBO;
import com.banmao.mall.model.bo.client.cart.CartUpdateBO;
import com.banmao.mall.model.pojo.*;
import com.banmao.mall.model.vo.client.cart.CartCheckoutVO;
import com.banmao.mall.model.vo.client.cart.CartIndexVO;
import com.banmao.mall.model.vo.client.cart.CartTotal;
import com.banmao.mall.service.client.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public CartIndexVO getCartList(Integer userId) {
        List<Cart> cartList = cartMapper.getCartList(userId);
        Integer goodsCount = cartMapper.getGoodsCount(userId);
        Integer checkedGoodsCount = cartMapper.getCheckedGoodsCount(userId);
        Integer goodsAmount = 0;
        for (Cart cart : cartList) {
            int price = cart.getPrice().intValue();
            goodsAmount += cart.getNumber() * price;
        }
        List<Cart> cartList1 = cartMapper.getCartList1(userId);
        Integer checkedGoodsAmount = 0;
        for (Cart cart : cartList1) {
            int price = cart.getPrice().intValue();
            checkedGoodsAmount += cart.getNumber() * price;
        }
        CartTotal cartTotal = new CartTotal();
        cartTotal.setGoodsCount(goodsCount);
        cartTotal.setCheckedGoodsCount(checkedGoodsCount);
        cartTotal.setGoodsAmount(goodsAmount);
        cartTotal.setCheckedGoodsAmount(checkedGoodsAmount);
        CartIndexVO cartIndexVO = new CartIndexVO();
        cartIndexVO.setCartList(cartList);
        cartIndexVO.setCartTotal(cartTotal);
        return cartIndexVO;
    }

    @Override
    public Boolean addCart(CartAddBO cartAddBO, Integer userId) {
        Cart cart1 = new Cart();
        cart1.setUserId(userId);
        cart1.setProductId(cartAddBO.getProductId());
        cart1.setGoodsId(cartAddBO.getGoodsId());
        List<Cart> cartList =cartMapper.selectByCart(cart1);
        int affectedRows = 0;
        GoodsProduct goodsProduct1 = goodsProductMapper.selectByPrimaryKey(cartAddBO.getProductId());
        if (cartList.size() != 0){
            Cart cart = cartList.get(0);
            int number = cart.getNumber() + cartAddBO.getNumber();
            if (goodsProduct1.getNumber() < number){
                return false;
            }
            cart.setNumber((short)number);
            cart.setUpdateTime(new Date());
            affectedRows = cartMapper.updateByPrimaryKeySelective(cart);
        }else {
            Goods goods = goodsMapper.selectByPrimaryKey(cartAddBO.getGoodsId());
            GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(cartAddBO.getProductId());
            Cart cart = new Cart();
            cart.setAddTime(new Date());
            cart.setUpdateTime(new Date());
            cart.setGoodsId(cartAddBO.getGoodsId());
            cart.setGoodsSn(goods.getGoodsSn());
            cart.setGoodsName(goods.getName());
            cart.setProductId(cartAddBO.getProductId());
            cart.setPrice(goodsProduct.getPrice());
            cart.setNumber(cartAddBO.getNumber());
            cart.setSpecifications(goodsProduct.getSpecifications());
            cart.setChecked(true);
            cart.setPicUrl(goods.getPicUrl());
            cart.setDeleted(false);
            cart.setUserId(userId);

            if (goodsProduct.getNumber() < cartAddBO.getNumber()){
                return false;
            }
            affectedRows = cartMapper.insert(cart);
        }
        if (affectedRows > 0){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public Boolean checkedCart(CartCheckedBO cartCheckedBO, Integer userId) {
        List<Integer> productIds = cartCheckedBO.getProductIds();
        Integer result = 0;
        for (Integer productId : productIds) {
            Cart cart = cartMapper.selectByUserIdAndProductId(productId, userId);
            cart.setChecked(cartCheckedBO.getIsChecked());
            cart.setUpdateTime(new Date());
            cart.setUserId(userId);
            result = cartMapper.updateByPrimaryKeySelective(cart);
        }
        if (result == 0){
            return false;
        }
        return true;
    }

    @Override
    public Integer cartGoodsCount(Integer userId) {
        Integer goodsCount = cartMapper.getGoodsCount(userId);
        return goodsCount;
    }

    @Override
    public Integer cartDelete(Integer userId, List<Integer> productIds) {
        Integer result = 0;
        for (Integer productId : productIds) {
            result += cartMapper.deleteByProductId(userId, productId);
        }

        return result;
    }

    @Override
    public Integer cartUpdate(Integer userId, CartUpdateBO cartUpdateBO) {
        GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(cartUpdateBO.getProductId());
        if (cartUpdateBO.getNumber() > goodsProduct.getNumber()){
            return 0;
        }
        Cart cart = new Cart();
        cart.setId(cartUpdateBO.getId());
        cart.setUpdateTime(new Date());
        cart.setNumber(cartUpdateBO.getNumber());
        int update = cartMapper.updateByPrimaryKeySelective(cart);
        return update;
    }

    @Override
    public CartCheckoutVO checkout(Integer userId, Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId) {
        //根据addressId查询地址
        Address address = addressMapper.selectByPrimaryKey(addressId);
        //根据grouponRulesId查询团购规则
        GrouponRules grouponRules = grouponRulesMapper.selectByPrimaryKey(grouponRulesId);
        CartCheckoutVO cartCheckoutVO = new CartCheckoutVO();
        //团购不做
        cartCheckoutVO.setGrouponPrice(0);
        cartCheckoutVO.setGrouponRulesId(grouponRulesId);
        //插入地址
        cartCheckoutVO.setAddressId(addressId);
        cartCheckoutVO.setCheckedAddress(address);
        List<Cart> cartList = null;
        //根据cartId判断是下单还是fastadd
        if (cartId == 0){
            cartList = cartMapper.selectByChecked();
        }else {
            cartList = cartMapper.selelctByCartId(cartId);
        }
        cartCheckoutVO.setCheckedGoodsList(cartList);
        BigDecimal goodsTotalPrice = new BigDecimal(0);
        for (Cart cart : cartList) {
            goodsTotalPrice = goodsTotalPrice.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
        }
        cartCheckoutVO.setGoodsTotalPrice(goodsTotalPrice.intValue());
        //优惠卷
        cartCheckoutVO.setCouponId(couponId);
        if (couponId > 0) {
            Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
            if (goodsTotalPrice.intValue() < coupon.getMin().intValue()){
                cartCheckoutVO.setCouponPrice(0);
            }else {
                cartCheckoutVO.setCouponPrice(coupon.getDiscount().intValue());
            }
        } else {
            cartCheckoutVO.setCouponPrice(0);
        }
        //conponlenth
        Integer availableCouponLength = couponUserMapper.getAvailableCouponLength(userId);
        cartCheckoutVO.setAvailableCouponLength(availableCouponLength);
        //goodsTotalPrice

        //freightPrice邮费
        Integer freightValue = 8;
        int freight_min = 88;
        if (goodsTotalPrice.intValue() >= freight_min){
            cartCheckoutVO.setFreightPrice(0);
        }else {
            cartCheckoutVO.setFreightPrice(freightValue);
        }

        Integer actualPrice = goodsTotalPrice.intValue() + cartCheckoutVO.getFreightPrice() - cartCheckoutVO.getCouponPrice() - cartCheckoutVO.getGrouponPrice();
        cartCheckoutVO.setActualPrice(actualPrice);
        cartCheckoutVO.setOrderTotalPrice(actualPrice);
        return cartCheckoutVO;
    }

    @Override
    public Integer getCartCount(Integer userId) {
        List<Cart> cartList = cartMapper.selectByUserId(userId);
        Integer cartCount = 0;
        for (Cart cart : cartList) {
            cartCount +=cart.getNumber();
        }
        return cartCount;
    }

    @Override
    public Integer fastAdd(CartAddBO cartAddBO, Integer userId) {

        Goods goods = goodsMapper.selectByPrimaryKey(cartAddBO.getGoodsId());
        GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(cartAddBO.getProductId());
        //判断剩余库存
        if (cartAddBO.getNumber() > goodsProduct.getNumber() ){
            return null;
        }
        Cart cart = new Cart();
        cart.setAddTime(new Date());
        cart.setUpdateTime(new Date());
        cart.setGoodsId(cartAddBO.getGoodsId());
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setGoodsName(goods.getName());
        cart.setProductId(cartAddBO.getProductId());
        cart.setPrice(goodsProduct.getPrice());
        cart.setNumber(cartAddBO.getNumber());
        cart.setSpecifications(goodsProduct.getSpecifications());
        cart.setChecked(true);
        cart.setPicUrl(goods.getPicUrl());
        cart.setDeleted(false);
        cart.setUserId(userId);
        int affectedRows = 0;
        List<Cart> cartList = cartMapper.selectByCart(cart);
        if (cartList.size() != 0){
            cart.setId(cartList.get(0).getId());
            affectedRows = cartMapper.updateByPrimaryKey(cart);
            if (affectedRows == 0){
                return null;
            }
            return cart.getId();
        }
        affectedRows = cartMapper.insert(cart);
        if (affectedRows == 0){
            return null;
        }
        return cart.getId();
    }
}
